<template>
    <section>
        <!--高级查询-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="product">
                <el-form-item>
                    <el-input v-model="product.name" placeholder="商品名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="searchDepartment">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="insertSubmit">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onStateSubmit" :disabled="this.sels.length===0">上架</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="downStateSubmit" :disabled="this.sels.length===0">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--员工列表加载-->
        <el-table :data="products" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <!--type="selection"选择多行数据-->
            <el-table-column type="selection">
            </el-table-column>
            <el-table-column type="index" align="center" label="序号" width="80">
            </el-table-column>
            <el-table-column prop="name" align="center" label="商品名称">
            </el-table-column>
            <el-table-column prop="saleprice" align="center" label="售价">
            </el-table-column>
            <el-table-column prop="costprice" align="center" label="成本价">
            </el-table-column>
            <el-table-column prop="state" align="center" label="状态">
                <template scope="scope">
                    <span style="color: green" v-if="scope.row.state==1">上架</span>
                    <span style="color: red" v-if="scope.row.state==0">下架</span>
                </template>
            </el-table-column>
            <el-table-column prop="salecount" align="center" label="销量">
            </el-table-column>
            <el-table-column prop="createtime" align="center" label="创建时间">
            </el-table-column>
            <el-table-column prop="onsaletime" align="center" label="上架时间">
            </el-table-column>
            <el-table-column prop="offsaletime" align="center" label="下架时间">
            </el-table-column>
            <el-table-column label="操作" width="150" >
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--分页和批量删除-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--增加和修改模态框-->
        <el-dialog title="修改" :visible.sync="dialogVisible"  width="30%">
            <!--修改弹窗-->
            <el-form :model="productForm" :rules="rules" ref="productForm" label-width="100px">
                <el-form-item label="商品名称" >
                    <el-input v-model="productForm.name"></el-input>
                </el-form-item>
                <el-form-item label="售价" >
                    <el-input v-model="productForm.saleprice"></el-input>
                </el-form-item>
                <el-form-item label="成本价" >
                    <el-input v-model="productForm.costprice"></el-input>
                </el-form-item>
                <el-form-item label="服务">
                    <el-upload
                            class="upload-demo"
                            action="http://localhost/fastDfs/upload"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="简介">
                    <!--<el-input v-model="productForm.productDetail.intro"></el-input>-->
                    <quill-editor v-model="productForm.productDetail.intro" :options="editorOption"></quill-editor>
                </el-form-item>
                <el-form-item label="预约须知" >
                    <!--<el-input v-model="productForm.productDetail.orderNotice"></el-input>-->
                    <quill-editor v-model="productForm.productDetail.orderNotice" :options="editorOption"></quill-editor>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click.native="addSubmit" >确 定</el-button>
          </span>
        </el-dialog>

    </section>
</template>
<script>
    import { quillEditor } from 'vue-quill-editor'
    import quillOption from '../../common/quill-config'
    export default {
        data() {
            return {
                editorOption:quillOption,
                //员工列表
                products:[],
                ids:[],
                //加载等待
                listLoading:false,
                pageSize:10,
                total:0,
                currentPage:1,
                sels:[],
                fileList: [],
                product:{
                    name:"",
                },
                dialogVisible:false,
                productForm:{
                    id:"",
                    name:"",
                    saleprice:"",
                    costprice:"",
                    productDetail:{
                        intro:"",
                        orderNotice:"",
                    },
                    resources:"",
                },
                rules: {

                }
            }
        },
        methods:{
            /*formatState(row,column){
                console.debug("-------------",row)
                return  row.state==1?"上架":"下架";
            },*/
            handlePreview(){

            },
            handleRemove(file, fileList){
                console.debug("==============",file)
                var url = "";
                //如果是增加中的删除图片
                if(file.response){
                    url=file.response.result;
                }else{//如果是编辑中的删除图片
                    url=file.url.substring(this.fastUrl.length);
                }
                let param = {
                    "url":url
                }
                this.$http.post("http://localhost/fastDfs/delete",param).then((res=>{
                    if(res.data.success){
                        //如果没有图片了，则直接将resources设置为空
                        if(fileList.length==0){
                            this.productForm.resources="";
                            return;
                        }
                        //如果还有图片遍历
                        for(var j=0;j<fileList.length;j++){
                            var fileObj =  fileList[j];
                            //如果为增加按钮
                            if(fileList.response){
                                if(j==1){
                                    this.productForm.resources=fileObj.response.result;
                                }else{
                                    this.productForm.resources=this.productForm.resources+","+fileObj.response.result;
                                }
                            }else {//如果为编辑按钮
                                if(j==0){
                                    //url: "http://121.37.194.36/group1/M00/00/07/wKgABF7bN9aAUr5jAABMYp1lVaA616.jpg"
                                    this.productForm.resources=fileObj.url.substring(this.fastUrl.length);
                                }else{//上传几张图片地址用，隔开
                                    this.productForm.resources=this.productForm.resources+","+fileObj.url.substring(this.fastUrl.length)
                                }
                            }
                        }
                        console.debug("---------",this.productForm.resources);
                    }
                }));
            },
            handleSuccess(response, file, fileList){
                console.debug(1,response)
                console.debug(2,file)
                console.debug(3,fileList)
                //this.productForm.resources = response.result;
                //遍历每张图片
                for (var i=0;i<fileList.length;i++){
                    //获取单张图片
                    var fileObj = fileList[i];
                    //如果存在response,则为增加服务图片
                    if(fileObj.response){
                        //如果只上传一张图片,直接上传地址
                        if(i==0){
                            this.productForm.resources=fileObj.response.result;
                        }else{//上传几张图片地址用，隔开
                            this.productForm.resources=this.productForm.resources+","+fileObj.response.result
                        }
                    }else{//没有response为编辑增加图片
                       if(i==0){
                            //url: "http://121.37.194.36/group1/M00/00/07/wKgABF7bN9aAUr5jAABMYp1lVaA616.jpg"
                            this.productForm.resources=fileObj.url.substring(this.fastUrl.length);
                        }else{//上传几张图片地址用，隔开
                            this.productForm.resources=this.productForm.resources+","+fileObj.url.substring(this.fastUrl.length)
                        }
                    }
                }
            },
            //多行改变事件
            selsChange(sels){
                console.debug(sels)
                this.sels=sels;
            },
            //查询
            searchDepartment(){
                this.loadProductsByQuery();
            },
            //增加
            insertSubmit(){
                //弹出模态框
                 this.dialogVisible=true;
                 this.fileList=[];
                    for (var k in this.productForm){
                        if(this.productForm[k]!=this.productForm.productDetail){
                            this.productForm[k] = "";
                        }
                    }
                this.productForm.productDetail.intro="";
                this.productForm.productDetail.orderNotice="";
            },
            //编辑事件
            handleEdit(index,row){
                this.dialogVisible=true;
                this.fileList=[];
                for (var k in row) {
                    if(this.productForm[k]!=this.productForm.productDetail) {
                        this.productForm[k] = row[k];
                    }
                }
                if(row.resources){
                    //逗号分隔每张图片
                    var imgArr = row.resources.split(",");
                    for(var a=0;a<imgArr.length;a++){
                        this.fileList.push({"url":this.fastUrl+imgArr[a]});
                    }
                }
                this.productForm.productDetail.intro=row.productDetail.intro;
                this.productForm.productDetail.orderNotice=row.productDetail.orderNotice;
            },
            //弹出框确定按钮
            addSubmit(){
                this.$refs["productForm"].validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //let param = this.productForm;
                            let param = Object.assign({},this.productForm);
                                if(param.id){
                                    this.$http.post("http://localhost/product",param).then((result=>{
                                        if(result.data.success){
                                            this.$message({
                                                type: 'success',
                                                message: '修改成功!'
                                            });
                                            this.loadProductsByQuery();
                                        }else {
                                            this.$message.error(result.data.msg);
                                        }
                                        this.dialogVisible=false;
                                    }))
                                }else{
                                    this.$http.put("http://localhost/product",param).then((result=>{
                                        if(result.data.success){
                                            this.$message({
                                                type: 'success',
                                                message: '增加成功!'
                                            });
                                            this.loadProductsByQuery();
                                        }else {
                                            this.$message.error(result.data.msg);
                                        }
                                        this.dialogVisible=false;
                                    }))
                                }
                        })
                    }
                })

            },
            //删除事件
            handleDel(index,row){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送请求，传入id参数
                    this.$http.delete("http://localhost/product/"+row.id).then((result=>{
                        if(result.data.success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.loadProductsByQuery();
                        }else{
                            this.$message.error(result.data.msg);
                        }
                    }))
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //批量删除
            batchRemove(){
                this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送请求，传入id参数
                    for (var s in this.sels){
                         this.ids[s] = this.sels[s].id;
                    }
                    this.$http.patch("http://localhost/product/batchRemove",this.ids).then((result=>{
                        console.debug(result)
                        if(result.data.success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.loadProductsByQuery();
                        }else{
                            this.$message.error(result.data.msg);
                        }
                    }))
                })
            },
            //改变当前页事件
            handleCurrentChange(val){
                this.currentPage=val;
                this.loadProductsByQuery();
            },
            //加载列表数据
            loadProductsByQuery(){
                //分页参数
                let param = {"currentPage":this.currentPage,"pageSize":this.pageSize,"name":this.product.name,"state":this.product.state}
                this.$http.patch("http://localhost/product",param).then((result)=>{
                        console.debug(result)
                        //后台返回员工数据列表
                        this.products=result.data.data;
                        //后台返回员工数据条数
                        this.total = result.data.total;
                })
            },
            //上架
            onStateSubmit(){
                this.$confirm('此操作将上架该商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送请求，传入id参数
                   /*for (var s in this.sels){
                        this.ids[s] = this.sels[s].id;
                    }
                    let param={
                        ids:this.ids,
                    }*/
                    var ids = this.sels.map(item=>item.id)
                    this.$http.patch("http://localhost/product/onState",ids).then((result=>{
                        console.debug(result)
                        if(result.data.success){
                            this.$message({
                                type: 'success',
                                message: '修改成功!'
                            });
                            this.loadProductsByQuery();
                        }else{
                            this.$message.error(result.data.msg);
                        }
                    }))
                })
            },
            //下架
            downStateSubmit(){
                this.$confirm('此操作将上架该商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送请求，传入id参数
                    /*for (var s in this.sels){
                        this.ids[s] = this.sels[s].id;
                    }*/
                    var ids = this.sels.map(item=>item.id)
                    this.$http.patch("http://localhost/product/downState",ids).then((result=>{
                        console.debug(result)
                        if(result.data.success){
                            this.$message({
                                type: 'success',
                                message: '修改成功!'
                            });
                            this.loadProductsByQuery();
                        }else{
                            this.$message.error(result.data.msg);
                        }
                    }))
                })
            },
        },

        mounted(){
            this.loadProductsByQuery();
        }
    }
</script>